//
//  AppDelegate.m
//  Manager
//
//  Created by Mr.Lee on 2018/4/3.
//  Copyright © 2018年 Reabam. All rights reserved.
//

#import "AppDelegate.h"
#import "IQKeyboardManager.h"
#import "TSBaseNavigationController.h"
#import "LoginViewController.h"
#import "HomeViewController.h"
#import "UMShare/UMShare.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    // 
    // 1 设置窗口
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;

//    LoginViewController *logVC = [[LoginViewController alloc] init];
//    TSBaseNavigationController *nav=[[TSBaseNavigationController alloc] initWithRootViewController:logVC];
//    [nav setNavigationBarHidden:YES];
//    [self.window setRootViewController:nav];

    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* tokenId = [user objectForKey:@"tokenId"];
    // 2 设置窗口的根控制器
    if (tokenId == nil && tokenId.length == 0) {
        LoginViewController *logVC = [[LoginViewController alloc] init];
        TSBaseNavigationController *nav=[[TSBaseNavigationController alloc] initWithRootViewController:logVC];
        [nav setNavigationBarHidden:YES];
        [self.window setRootViewController:nav];
    //  2 设置窗口的根控制器
    } else{
        [self setupViewControllers];
        [self.window setRootViewController:self.viewController];
    }
    
    // 3 显示窗口
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    
    // done --> 完成
    [IQKeyboardManager sharedManager].toolbarDoneBarButtonItemText = @"完成";
    [IQKeyboardManager sharedManager].toolbarTintColor = CommonColor;
    
    // 延时(广告图)
    //    [NSThread sleepForTimeInterval:2.0];
    
    
    // 友盟
    [self configUSharePlatforms];
    [self confitUShareSettings];
    
    // 状态栏
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    
    
    return YES;
}



- (void)confitUShareSettings
{
    /*
     * 打开图片水印
     */
    //[UMSocialGlobal shareInstance].isUsingWaterMark = YES;
    /*
     * 关闭强制验证https，可允许http图片分享，但需要在info.plist设置安全域名
     <key>NSAppTransportSecurity</key>
     <dict>
     <key>NSAllowsArbitraryLoads</key>
     <true/>
     </dict>
     */
    //[UMSocialGlobal shareInstance].isUsingHttpsWhenShareContent = NO;
}

- (void)configUSharePlatforms
{
    NSString *str1=[NSString stringWithFormat:@"%d",TARGET_VERSION];
    NSString *plistPath1 = [[NSBundle mainBundle] pathForResource:str1 ofType:@"plist"];
    NSDictionary *dictionary1 = [[NSDictionary alloc] initWithContentsOfFile:plistPath1];
    NSString *setWXAppId = [dictionary1 objectForKey:@"setWXAppId"];
    NSString *appSecret = [dictionary1 objectForKey:@"appSecret"];
    NSString *appKey = [dictionary1 objectForKey:@"appkey"];
    [[UMSocialManager defaultManager] setUmSocialAppkey:appKey];
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession | UMSocialPlatformType_WechatTimeLine appKey:setWXAppId appSecret:appSecret redirectURL:@"http://www.reabam.com"];

}


// 设置所有控制器
- (void)setupViewControllers {

    // 首页
    HomeViewController *firstViewController = [[HomeViewController alloc] init];
    firstViewController.title = @"管理";
    TSBaseNavigationController *firstNavigationController = [[TSBaseNavigationController alloc]
                                                             initWithRootViewController:firstViewController];
    
    // 发现
    DiscoverHouseholdListViewController *fourthViewController = [[DiscoverHouseholdListViewController alloc] init];
    fourthViewController.title = @"发现";
    TSBaseNavigationController *fourthNavigationController = [[TSBaseNavigationController alloc]
                                                              initWithRootViewController:fourthViewController];
    
    // 我的
    MyViewController *fifthViewController = [[MyViewController alloc] init];
    TSBaseNavigationController *fifthNavigationController = [[TSBaseNavigationController alloc]
                                                             initWithRootViewController:fifthViewController];
    fifthNavigationController.title = @"我的";
    
    
    // 设置tabbar的子控制器
    RDVTabBarController *tabBarController = [[RDVTabBarController alloc] init];
    tabBarController.view.backgroundColor = [UIColor whiteColor];
    [tabBarController setViewControllers:@[firstNavigationController,
                                           fourthNavigationController, fifthNavigationController]];
    self.viewController = tabBarController;
    // 设置tabbar
    [self customizeTabBarForController:tabBarController];
}
- (void)customizeTabBarForController:(RDVTabBarController *)tabBarController {
    //    设置选中或非选中tabbar背景图片
    //    UIImage *finishedImage = [UIImage imageNamed:@"tabbar_selected_background"];
    //    UIImage *unfinishedImage = [UIImage imageNamed:@"tabbar_normal_background"];
    //    [item setBackgroundSelectedImage:finishedImage withUnselectedImage:unfinishedImage];
    
    // 设置tabbar Item
    NSArray *tabBarItemImages = @[@"guanli", @"faxian", @"wode"];
    NSInteger index = 0;
    for (RDVTabBarItem *item in [[tabBarController tabBar] items]) {
        
        
        // 设置选中或者非选中title颜色
        NSDictionary *tabBarTitleUnselectedDic = @{NSForegroundColorAttributeName:COLOR(153,153,153), NSFontAttributeName:[UIFont systemFontOfSize:12]};
        NSDictionary *tabBarTitleSelectedDic = @{NSForegroundColorAttributeName:CommonColor,NSFontAttributeName:[UIFont systemFontOfSize:12]};
        //修改tabberItem的title颜色
        item.selectedTitleAttributes = tabBarTitleSelectedDic;
        item.unselectedTitleAttributes = tabBarTitleUnselectedDic;
        
        
        // 设置item图片和文字之间的间距
        //        item.titlePositionAdjustment =  UIOffsetMake(0, 4.5);
        
        // 选中图片/非选中图片
        UIImage *selectedimage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_hight",
                                                      [tabBarItemImages objectAtIndex:index]]];
        UIImage *unselectedimage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_normal",
                                                        [tabBarItemImages objectAtIndex:index]]];
        [item setFinishedSelectedImage:selectedimage withFinishedUnselectedImage:unselectedimage];
        
        index++;
    }
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    
     [[NSNotificationCenter defaultCenter]postNotificationName:@"NewVersion" object:nil];
    
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    // Saves changes in the application's managed object context before the application terminates.
}


// 支持所有iOS系统
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    //6.3的新的API调用，是为了兼容国外平台(例如:新版facebookSDK,VK等)的调用[如果用6.2的api调用会没有回调],对国内平台没有影响
    BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url sourceApplication:sourceApplication annotation:annotation];
    if (!result) {
        // 其他如支付等SDK的回调
    }
    return result;
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
    if (!result) {
        // 其他如支付等SDK的回调
    }
    return result;
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options
{
    //6.3的新的API调用，是为了兼容国外平台(例如:新版facebookSDK,VK等)的调用[如果用6.2的api调用会没有回调],对国内平台没有影响
    BOOL result = [[UMSocialManager defaultManager]  handleOpenURL:url options:options];
    if (!result) {
        // 其他如支付等SDK的回调
    }
    return result;
}

@end
